﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel.Description;
using System.ServiceModel.Dispatcher;
using System.ServiceModel.Channels;

namespace Obra.SmugMug.Api
{
    /// <summary>
    /// Modify the endpoint's behavior
    /// </summary>
    /// <remarks>
    /// <para>
    /// This class does the following.
    ///   + Adds message inspectors to the clients - let's one look at the
    ///     request, and response messages.
    /// </para>
    /// </remarks>
    public class EndpointBehavior : 
        IEndpointBehavior
    {
        #region IEndpointBehavior Members

        /// <summary>
        /// Implement to pass data at runtime to bindings to support custom 
        /// behavior.
        /// </summary>
        /// <param name="endpoint">
        /// The endpoint to modify.
        /// </param>
        /// <param name="bindingParameters">
        /// The objects that binding elements require to support the behavior.
        /// </param>
        public void AddBindingParameters(
            ServiceEndpoint endpoint, 
            BindingParameterCollection bindingParameters)
        {
        }

        /// <summary>
        /// Implements a modification or extension of the client across an 
        /// endpoint.
        /// </summary>
        /// <param name="endpoint">
        /// The endpoint that is to be customized.
        /// </param>
        /// <param name="clientRuntime">
        /// The client runtime to be customized.
        /// </param>
        public void ApplyClientBehavior(
            ServiceEndpoint endpoint, 
            ClientRuntime clientRuntime)
        {
            clientRuntime.MessageInspectors.Add(
                new MessageInspector());

            // All of the operations need the same formatter.
            foreach (var operation in endpoint.Contract.Operations)
            {
                operation.Behaviors.Add(
                    new MessageFormatter());
            }
        }

        /// <summary>
        /// Implements a modification or extension of the service across an 
        /// endpoint.
        /// </summary>
        /// <param name="endpoint">
        /// The endpoint that exposes the contract.
        /// </param>
        /// <param name="endpointDispatcher">
        /// The endpoint dispatcher to be modified or extended.
        /// </param>
        public void ApplyDispatchBehavior(
            ServiceEndpoint endpoint, 
            EndpointDispatcher endpointDispatcher)
        {
        }

        /// <summary>
        /// Implement to confirm that the endpoint meets some intended 
        /// criteria.
        /// </summary>
        /// <param name="serviceEndpoint">
        /// The endpoint to validate.
        /// </param>
        public void Validate(ServiceEndpoint endpoint)
        {
        }

        #endregion
    }
}
